package com.muchfish.reactor.api;

import reactor.core.publisher.Flux;

public class ReactorFluxDeferExample {
    public static void main(String[] args) {
        Flux<Integer> deferredFlux = Flux.defer(() -> {
            // 在每次订阅时创建新的 Flux
            System.out.println("Creating new Flux");
            return Flux.just(1, 2, 3);
        });

        // 第一个订阅
        deferredFlux.subscribe(data -> System.out.println("Subscriber 1: " + data));

        // 第二个订阅
        deferredFlux.subscribe(data -> System.out.println("Subscriber 2: " + data));

        // 结果：
        // Creating new Flux
        // Subscriber 1: 1
        // Subscriber 1: 2
        // Subscriber 1: 3
        // Creating new Flux
        // Subscriber 2: 1
        // Subscriber 2: 2
        // Subscriber 2: 3
    }
}
